<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增商品分类')"/>

    <link th:href="@{/ajax/libs/layui/css/layui.css}" rel="stylesheet"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-category-add">

        <div class="form-group">
            <label class="col-sm-3 control-label">名称
                ：</label>
            <div class="col-sm-8">
                <input name="categoryName" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">是否显示：</label>
            <div class="col-sm-8">

                <div class="radio-box" th:each="dict : ${@dict.getType('common_yes_no')}">
                    <input type="radio" th:id="${'display' + dict.dictCode}" name="display"
                           th:value="${dict.dictValue}"
                           th:checked="${dict.default}" required>
                    <label th:for="${'display' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">排序
                ：</label>
            <div class="col-sm-8">
                <input name="categorySort" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">上级分类：</label>
            <div class="col-sm-8">
                <div class="input-group">
                    <input id="treeId" name="parentId" type="hidden" th:value="${category?.id}"/>
                    <input class="form-control" type="text" onclick="selectCategoryTree()"
                           id="treeName" readonly="true" th:value="${category?.categoryName}">
                    <span class="input-group-addon"><i class="fa fa-search"></i></span>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">分类图片</label>
            <div class="col-sm-8" style="display: flex;">
                <div class="layui-upload">
                    <button type="button" class="layui-btn" id="categoryUpload">上传图片</button>
                    <div class="layui-upload-list">
                        <input type="hidden" name="iconUrl" id="iconUrl">
                        <img class="layui-upload-img" id="demo1">
                        <p id="demoText"></p>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<script th:src="@{/ajax/libs/layui/layui.js}"></script>
<script type="text/javascript">

    layui.use('upload', function () {
        var $ = layui.jquery;
        var upload = layui.upload;
        var uploadInst = upload.render({
            elem: '#categoryUpload',
            url: ctx + 'common/upload', //改成您自己的上传接口
            before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    //$('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.code > 0) {
                    return layer.msg('上传失败');
                }
                //上传成功
                $('#demo1').attr('src', res.url);
                $('#iconUrl').val(res.url);
            }
            , error: function () {
                //演示失败状态，并实现重传
                return layer.msg('上传失败');
            }
        });
    });
    var prefix = ctx + "product/category"
    $("#form-category-add").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            $.operate.save(prefix + "/add", $('#form-category-add').serialize());
        }
    }

    /*商品分类-新增-选择父部门树*/
    function selectCategoryTree() {
        var pid = $("#treeId").val() == null ? 0 : $("#treeId").val();
        var options = {
            title: '商品分类选择',
            width: "380",
            url: prefix + "/selectCategoryTree/" + pid,
            callBack: doSubmit
        };
        $.modal.openOptions(options);
    }

    function doSubmit(index, layero) {
        var body = layer.getChildFrame('body', index);
        $("#treeId").val(body.find('#treeId').val());
        $("#treeName").val(body.find('#treeName').val());
        layer.close(index);
    }

    function fileSelectCallBack(index, layero) {

    }


</script>
</body>
</html>